package com.atguigu.orderservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.orderservice.service.PayLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-22
 */
@CrossOrigin
@RestController
@RequestMapping("/eduorder/paylog")
public class PayLogController {

    @Autowired
    PayLogService payLogService;

    @ApiOperation(value = "生成二维码")
    @GetMapping("createNative/{orderNO}")
    public R createNative(@PathVariable String orderNO){

        Map<String, Object> map =  payLogService.createNative(orderNO);

        return  R.ok().data(map);
    }

    //查询订单支付状态
    //参数：订单号，根据订单号查询 支付状态

    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo){
        Map<String,String> map =  payLogService.queryPayStatus(orderNo);
        System.out.println("*******查询订单状态map "+ map);

        if (map ==null){
            return R.error().message("支付错误");

        }

        if (map.get("trade_state").equals("SUCCESS")){
            payLogService.updateOrdersStatus(map);

            return R.ok().message("支付成功");
        }
        return  R.ok().code(25000).message("支付中");
    }

}

